package com.oyjd.fw.ws;

import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.net.httpserver.HttpExchange;

/**
 * 服务端数据链处理示例
 * 
 * @author 欧建东 <B>Email oyjd@163.com</B>
 * 
 */
public class ServerHandler implements SOAPHandler<SOAPMessageContext> {

	public boolean handleMessage(SOAPMessageContext context) {
		boolean outboundProperty = ((Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
		SOAPMessage message = context.getMessage();
		// ----------------------------------遍历内容key
		for(Iterator<String> item=context.keySet().iterator();item.hasNext();){
			String key=item.next();
			System.out.println("-------->"+key+" : "+context.get(key));
		}
		
		try {			
			if (outboundProperty) {
				System.out.println("服务器回复请求==========>");
			} else {				
				// ----------------------------------获取head
				SOAPHeader head = message.getSOAPHeader();
				NodeList nodeList = head.getChildNodes();
				System.out.println("Head 节点数量:" + nodeList.getLength());
				String userId = "";
				String pwd = "";
				for (int i = 0; i < nodeList.getLength(); i++) {
					Node node = nodeList.item(i);
					if ("security".equals(node.getLocalName())) {
						NodeList tmp = node.getChildNodes();

						for (int j = 0; j < tmp.getLength(); j++) {
							Node t = tmp.item(j);
							if ("userId".equals(t.getLocalName())) {
								userId = t.getTextContent();
							} else if ("pwd".equals(t.getLocalName())) {
								pwd = t.getTextContent();
							}
						}
					}

				}
				System.out.println("check---->userId:" + userId + " pwd:" + pwd);
				
				// ------------------------------获取请求
				HttpExchange exchange = (HttpExchange)context.get("com.sun.xml.internal.ws.http.exchange"); 
				InetSocketAddress isa = exchange.getRemoteAddress(); 
				String ip=isa.getAddress().getHostAddress();				
				System.out.println("request ip:--->" + ip);
				
				//--------------循环context内所有key,发现根本不含MessageContext.SERVLET_REQUEST，所以request始终为null
				HttpServletRequest request = (HttpServletRequest) context.get(MessageContext.SERVLET_REQUEST);
				if (null != request) {
					System.out.println("url--->" + request.getRequestURL());
					System.out.println("request ip:--->" + request.getRemoteAddr());
				}
				System.out.println("服务器收到请求==========>");
			}
			message.writeTo(System.out);
			System.out.println("");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean handleFault(SOAPMessageContext context) {
		// TODO Auto-generated method stub
		return false;
	}

	public void close(MessageContext context) {

	}

	public Set<QName> getHeaders() {
		return null;
	}

}
